package com.pdg.mcplugin.invisiblehand.dataproviders;

import java.sql.ResultSet;

import org.bukkit.inventory.ItemStack;

import com.pdg.mcplugin.common.interfaces.TableProviderValue;

public class Commodity implements TableProviderValue<Commodity>{

	private static final long stockInfinite = -1;
	private static final double markUpFactor = 100.0;
	private static final double priceZero = 0;
	private static final int indexZero = 0;
	private static final double markUpBase = 1.0;
	
	private String name;
	private ItemStack itemStack;
	private double basePrice;
	private double baseSupply;
	private double baseDemand;
	private double supplyFactor;
	private double demandFactor;
	private double markUp;
	private long salesCount;
	private long purchaseCount;
	private long stock;
	private boolean enabled;
	private String world;
	public String getName() {
		return name;
	}
	public String getWorld(){
		return world;
	}

	public ItemStack getItemStack() {
		return itemStack;
	}
	public double getBasePrice() {
		return basePrice;
	}

	public double getBaseSupply() {
		return baseSupply;
	}

	public double getBaseDemand() {
		return baseDemand;
	}

	public double getSupplyFactor() {
		return supplyFactor;
	}

	public double getDemandFactor() {
		return demandFactor;
	}

	public double getMarkUp() {
		return markUp*markUpFactor;
	}

	public long getSalesCount() {
		return salesCount;
	}

	public long getPurchaseCount() {
		return purchaseCount;
	}
	
	public long getStock(){
		return stock;
	}

	public boolean getEnabled() {
		return enabled;
	}
	
	public Commodity(String theName, String theWorld, ItemStack theItemStack,double theBasePrice,double theBaseSupply, double theBaseDemand, double theSupplyFactor, double theDemandFactor,double theMarkUp, long theSalesCount, long thePurchaseCount,long theStock, boolean theEnabled){
		setName(theName);
		setWorld(theWorld);
		setItemStack(theItemStack);
		setBasePrice(theBasePrice);
		setBaseSupply(theBaseSupply);
		setBaseDemand(theBaseDemand);
		setSupplyFactor(theSupplyFactor);
		setDemandFactor(theDemandFactor);
		setMarkUp(theMarkUp);
		setSalesCount(theSalesCount);
		setPurchaseCount(thePurchaseCount);
		setStock(theStock);
		setEnabled(theEnabled);
	}

	public void setStock(long theStock) {
		stock = theStock;
	}
	private void setWorld(String theWorld) {
		world = theWorld;
	}

	public void setEnabled(boolean theEnabled) {
		enabled = theEnabled;
	}

	public void setPurchaseCount(long thePurchaseCount) {
		purchaseCount = thePurchaseCount;
	}

	public void setSalesCount(long theSalesCount) {
		salesCount = theSalesCount;
	}

	public void setMarkUp(double theMarkUp) {
		markUp = theMarkUp/markUpFactor;
	}

	public void setDemandFactor(double theDemandFactor) {
		demandFactor = theDemandFactor;
	}

	public void setSupplyFactor(double theSupplyFactor) {
		supplyFactor = theSupplyFactor;
	}

	public void setBaseDemand(double theBaseDemand) {
		baseDemand = theBaseDemand;
	}

	public void setBaseSupply(double theBaseSupply) {
		baseSupply = theBaseSupply;
	}

	public void setBasePrice(double theBasePrice) {
		basePrice = theBasePrice;
	}

	public void setItemStack(ItemStack theItemStack) {
		itemStack = theItemStack;
	}

	public void setName(String theName) {
		name = theName;
	}
	
	public long getAvailableStock(){
		if(getStock()==stockInfinite){
			return Long.MAX_VALUE;
		}else{
			return getStock()+getSalesCount()-getPurchaseCount();
		}
	}
	
	public double getSalesPrice(long theUnits){
		double theTotalPrice = priceZero;
		for(int theUnitsSold = indexZero;theUnitsSold<theUnits;++theUnitsSold){
			theTotalPrice += getBasePrice() * (getBaseDemand() + getDemandFactor() * (getPurchaseCount())) / (getBaseSupply()+getSupplyFactor()*(getSalesCount()+theUnitsSold));
		}
		return theTotalPrice;
	}
	
	public double getPurchasePrice(long theUnits){
		double theTotalPrice = priceZero;
		for(int theUnitsPurchased = indexZero;theUnitsPurchased<theUnits;++theUnitsPurchased){
			theTotalPrice += getBasePrice() * (markUpBase + getMarkUp()/markUpFactor) * (getBaseDemand() + getDemandFactor() * (getPurchaseCount()+theUnitsPurchased)) / (getBaseSupply()+getSupplyFactor()*(getSalesCount()));
		}
		return theTotalPrice;
	}
	@Override
	public String getCreateFields() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String getInsertFields() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String getInsertValues() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String getSelectList() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String getUpdateList() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Commodity createValueFromResultSet(ResultSet theResultSet) {
		// TODO Auto-generated method stub
		return null;
	}
	public static Commodity defaultInstance() {
		// TODO Auto-generated method stub
		return null;
	}

}
